package hk.org.ha.cdars.job;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import hk.org.ha.cdars.datacontext.BisDataContext.G1CriterialNode;
import hk.org.ha.cdars.datacontext.BisDataContext.G2CriterialNode;
import hk.org.ha.cdars.datacontext.BisDataContext.G2RefG1CheckResult;
import lava.rt.common.UtilCommon;
import lava.rt.linq.CommandExecuteExecption;

@Service
public class G2RefG1CheckJob extends CheckJob<G2RefG1CheckResult>{

	@Override
	public List<G2RefG1CheckResult> check() throws Exception {
		// TODO Auto-generated method stub
		List<G2RefG1CheckResult> rets=new ArrayList<>();
		// TODO Auto-generated method stub
		 
		List<G1CriterialNode> g1nodes=dataContext.G1_CRITERIAL_NODE.selectList("",CR.criteriaType.asc );
		
		Map<String, G1CriterialNode> g1nodeMap=UtilCommon.toMap(g1nodes, (v)->{ return v.getPath(); });
		List<String> diffMsgs=new ArrayList<>();
		dataContext.G2_CRITERIAL_NODE.selectCursoring((index,g2node)->{
			diffMsgs.clear();
			G2RefG1CheckResult ret=new G2RefG1CheckResult();
			ret.setG2Path(g2node.getPath());
			G1CriterialNode g1Node=g1nodeMap.get(g2node.getPath());
			if(g1Node==null) {
			   ret.setFlag(Flag.不存在.code);	
			}else if(compare(g1Node, g2node, diffMsgs)>0) {
				ret.setFlag(Flag.差異.code);
				ret.setRemark(String.join(";", diffMsgs));
			}
			else {
				ret.setFlag(Flag.相同.code);	
			}
			ret.setJobNo(getJobNo());
			ret.setTreeType(g2node.getTreeType());
			
			ret.setNodeType(getNodeType(g2node));
			
			rets.add(ret);
			return 0;
		},  CR.remark.notEq("warning"), CR.criteriaType.asc);
		
		return rets;
	}

	
	

}
